<template>

  <div class="container">
    <div class="comment" v-for="item in comments" :key="item.id">
      <div class="info">
        <img class="avatar" :src="item.fromAvatar" width="36" height="36" />
        <div class="right">
          <div class="name">{{ item.fromName }}</div>
          <div class="date">{{ item.date }}</div>
        </div>
      </div>
      <div class="content">{{ item.content }}</div>
      <div class="control">
        <span class="like" :class="{ active: item.isLike }" @click="likeClick(item)">
          <i class="iconfont icon-like"></i>
          <span class="like-num">{{ item.likeNum > 0 ? item.likeNum + '人赞' : '赞' }}</span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <i class="iconfont icon-comment"></i>
          <span>回复</span>
        </span>
      </div>
      <div class="reply">
        <div class="item" v-for="reply in item.reply" :key="reply.id">
          <div class="reply-content">
            <span class="from-name">{{ reply.fromName }}</span><span>: </span>
            <div v-if="reply.toName!= null">
              <span class="to-name">@{{ reply.toName }}</span>
            </div>
            <span>{{ reply.content }}</span>
          </div>
          <div class="reply-bottom">
            <span>{{ reply.date }}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <i class="iconfont icon-comment"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
        <div class="write-reply" v-if="item.reply.length > 0" @click="showCommentInput(item)">
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div>
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId === item.id">
            <el-input class="gray-bg-input" v-model="inputComment" type="textarea" :rows="3" autofocus
              placeholder="写下你的评论"></el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="commitComment(item, replyId)">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';
import { time } from 'console';

import { ElMessage } from 'element-plus';

interface CommentReply {
  id: number,  // 主键id (改为 int)
  commentId: number,  // 父评论id，即父评论的 id (改为 int)
  fromId: number,  // 评论者id (改为 int)
  fromName: string,  // 评论者昵称
  fromAvatar: string,  // 评论者头像
  toId: number | null,  // 被评论者id (改为 int)
  toName: string,  // 被评论者昵称
  toAvatar: string,  // 被评论者头像
  content: string,  // 评论内容
  date: string  // 评论时间
}

interface CommentItem {
  id: number; // 主键id
  date: string; // 评论时间
  ownerId: number; // 文章的id
  fromId: number; // 评论者id
  fromName: string; // 评论者昵称
  fromAvatar: string; // 评论者头像
  likeNum: number; // 点赞人数
  content: string; // 评论内容
  reply: CommentReply[];
}

interface Props {
  my_id: number;
  comments: CommentItem[];
}

const props = defineProps<Props>();
console.log("comment:", props.comments);

const inputComment = ref<string>('');
const showItemId = ref<number | null>(null);
const replyId = ref<number | null>(null);
const selectReply = ref<CommentReply | null>(null);
const likeClick = async (item: CommentItem) => {
  // 假设后端有点赞接口
  const res = await axios.post('/api/comments/like', { commentId: item.id });
  if (res.data.success) {
    if (item.isLike === null || item.isLike === false) {
      item.isLike = true;
      item.likeNum++;
    } else {
      item.isLike = false;
      item.likeNum--;
    }
  }
};

const cancel = () => {
  showItemId.value = null;
  inputComment.value = '';
  replyId.value = null;
};

const commitComment = async (item: CommentItem, replyId?: number) => {
  const payload = {
    bookId: item.id,
    content: inputComment.value,
    fromId: props.my_id,
    commentId: replyId ? item.id : null,
  };
  const myInput = inputComment.value.trim();
  var  inputcontent = myInput; 
// 找到第一个空格的位置
if(selectReply.value !== null){
  const firstSpaceIndex = myInput.indexOf(' ');
  inputcontent = myInput.substring(firstSpaceIndex + 1);

}


  const req = {

    commentId: item.id,  // 父评论id，即父评论的 id (改为 int)
    fromId: 1,  // 评论者id (改为 int)
    fromName: '作者',  // 评论者昵称
    fromAvatar: 'https://wx4.sinaimg.cn/mw690/69e273f8gy1ft1541dmb7j215o0qv7wh.jpg',  // 评论者头像
    toId: selectReply.value !== null ? selectReply.value.fromId : null,  // 被评论者id (改为 int)
    toName: selectReply.value !== null ? selectReply.value.fromName : null,  // 被评论者昵称
    toAvatar: selectReply.value !== null ? selectReply.value.fromAvatar : null,  // 被评论者头像
    content: inputcontent,  // 评论内容
    date: '2024-09-17T09:00:00'  // 评论时间
  }


  const url = '/api/comments/reply';
  const res = await axios.post(url, req);

  if (res.status === 200) {
    console.log('提交成功');
    ElMessage({
      message: '评论创建成功',
      type: 'success',
    });
    cancel();
  } else {
    console.log('提交失败');
    ElMessage({
      message: '书籍创建成功',
      type: 'error',
    });
  }
};

const showCommentInput = (item: CommentItem, reply?: CommentReply) => {
  selectReply.value = null;

  if (reply) {
    selectReply.value = reply;
    // inputComment.value = `@${reply.fromName} `;
    inputComment.value = `@${reply.fromName} `;
    replyId.value = reply.id;
  } else {
    inputComment.value = '';
    replyId.value = null;
  }
  showItemId.value = item.id;

};
</script>



<style scoped lang="scss">
@import "../../public/scss/index";

.container {
  padding: 0 10px;
  box-sizing: border-box;

  .comment {
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-bottom: 1px solid $border-fourth;

    .info {
      display: flex;
      align-items: center;

      .avatar {
        border-radius: 50%;
      }

      .right {
        display: flex;
        flex-direction: column;
        margin-left: 10px;

        .name {
          font-size: 16px;
          color: $text-main;
          margin-bottom: 5px;
          font-weight: 500;
        }

        .date {
          font-size: 12px;
          color: $text-minor;
        }
      }
    }

    .content {
      font-size: 16px;
      color: $text-main;
      line-height: 20px;
      padding: 10px 0;
    }

    .control {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: $text-minor;

      .like {
        display: flex;
        align-items: center;
        margin-right: 20px;
        cursor: pointer;

        &.active,
        &:hover {
          color: $color-main;
        }

        .iconfont {
          font-size: 14px;
          margin-right: 5px;
        }
      }

      .comment-reply {
        display: flex;
        align-items: center;
        cursor: pointer;

        &:hover {
          color: $text-333;
        }

        .iconfont {
          font-size: 16px;
          margin-right: 5px;
        }
      }
    }

    .reply {
      margin: 10px 0;
      border-left: 2px solid $border-first;

      .item {
        margin: 0 10px;
        padding: 10px 0;
        border-bottom: 1px dashed $border-third;

        .reply-content {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: $text-main;

          .from-name {
            color: $color-main;
          }

          .to-name {
            color: $color-main;
            margin-left: 5px;
            margin-right: 5px;
          }
        }

        .reply-bottom {
          display: flex;
          align-items: center;
          margin-top: 6px;
          font-size: 12px;
          color: $text-minor;

          .reply-text {
            display: flex;
            align-items: center;
            margin-left: 10px;
            cursor: pointer;

            &:hover {
              color: $text-333;
            }

            .icon-comment {
              margin-right: 5px;
            }
          }
        }
      }

      .write-reply {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: $text-minor;
        padding: 10px;
        cursor: pointer;

        &:hover {
          color: $text-main;
        }

        .el-icon-edit {
          margin-right: 5px;
        }
      }

      .fade-enter-active,
      fade-leave-active {
        transition: opacity 0.5s;
      }

      .fade-enter,
      .fade-leave-to {
        opacity: 0;
      }

      .input-wrapper {
        padding: 10px;

        .gray-bg-input,
        .el-input__inner {
          /*background-color: #67C23A;*/
        }

        .btn-control {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          padding-top: 10px;

          .cancel {
            font-size: 16px;
            color: $text-normal;
            margin-right: 20px;
            cursor: pointer;

            &:hover {
              color: $text-333;
            }
          }

          .confirm {
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
